import { hefengApiKey } from "./cons";

/**
 * 获取位置坐标，默认为杭州
 * @returns location
 */
const getLocation = () => {
  let location = `114.15,22.24`
  let options = {
    enableHighAccuracy: true,
    timeout: 5000,
    maximumAge: 0
  };
  return new Promise((resolve, reject) => {
    navigator.geolocation.getCurrentPosition((pos) => {
      let {
        longitude,
        latitude
      } = pos.coords;
      location = `${longitude.toFixed(2)},${latitude.toFixed(2)}`
      resolve(location)
    }, () => {
      resolve(location)
    }, options);
  })
}

/**
 * 实时天气
 * @returns 
 */
export const getRealTimeWeather = async () => {
  let res: any = {
    "code": "200",
    "updateTime": "2020-06-30T22:00+08:00",
    "fxLink": "http://hfx.link/2ax1",
    "now": {
      "obsTime": "2020-06-30T21:40+08:00",
      "temp": "24",
      "feelsLike": "26",
      "icon": "101",
      "text": "多云",
      "wind360": "123",
      "windDir": "东南风",
      "windScale": "1",
      "windSpeed": "3",
      "humidity": "72",
      "precip": "0.0",
      "pressure": "1003",
      "vis": "16",
      "cloud": "10",
      "dew": "21"
    },
    "refer": {
      "sources": [
        "QWeather",
        "NMC",
        "ECMWF"
      ],
      "license": [
        "commercial license"
      ]
    }
  }
  try {
    let location = await getLocation()

    // const resFetch = await fetch(`https://devapi.qweather.com/v7/weather/now?location=${location}&key=${hefengApiKey}`);
    // res = await resFetch.json()
    return res
  } catch (error) {

  }
}

/**
 * 每日天气预报
 * @returns 
 */
export const getWeatherForDay = async (
  day: 3 | 7 | 10 | 15 | 30
) => {

  let res: any = {
    "code": "200",
    "updateTime": "2021-11-15T16:35+08:00",
    "fxLink": "http://hfx.link/2ax1",
    "daily": [
      {
        "fxDate": "2021-11-15",
        "sunrise": "06:58",
        "sunset": "16:59",
        "moonrise": "15:16",
        "moonset": "03:40",
        "moonPhase": "盈凸月",
        "moonPhaseIcon": "803",
        "tempMax": "12",
        "tempMin": "-1",
        "iconDay": "101",
        "textDay": "多云",
        "iconNight": "150",
        "textNight": "晴",
        "wind360Day": "45",
        "windDirDay": "东北风",
        "windScaleDay": "1-2",
        "windSpeedDay": "3",
        "wind360Night": "0",
        "windDirNight": "北风",
        "windScaleNight": "1-2",
        "windSpeedNight": "3",
        "humidity": "65",
        "precip": "0.0",
        "pressure": "1020",
        "vis": "25",
        "cloud": "4",
        "uvIndex": "3"
      },
      {
        "fxDate": "2021-11-16",
        "sunrise": "07:00",
        "sunset": "16:58",
        "moonrise": "15:38",
        "moonset": "04:40",
        "moonPhase": "盈凸月",
        "moonPhaseIcon": "803",
        "tempMax": "13",
        "tempMin": "0",
        "iconDay": "100",
        "textDay": "晴",
        "iconNight": "101",
        "textNight": "多云",
        "wind360Day": "225",
        "windDirDay": "西南风",
        "windScaleDay": "1-2",
        "windSpeedDay": "3",
        "wind360Night": "225",
        "windDirNight": "西南风",
        "windScaleNight": "1-2",
        "windSpeedNight": "3",
        "humidity": "74",
        "precip": "0.0",
        "pressure": "1016",
        "vis": "25",
        "cloud": "1",
        "uvIndex": "3"
      },
      {
        "fxDate": "2021-11-17",
        "sunrise": "07:01",
        "sunset": "16:57",
        "moonrise": "16:01",
        "moonset": "05:41",
        "moonPhase": "盈凸月",
        "moonPhaseIcon": "803",
        "tempMax": "13",
        "tempMin": "0",
        "iconDay": "100",
        "textDay": "晴",
        "iconNight": "150",
        "textNight": "晴",
        "wind360Day": "225",
        "windDirDay": "西南风",
        "windScaleDay": "1-2",
        "windSpeedDay": "3",
        "wind360Night": "225",
        "windDirNight": "西南风",
        "windScaleNight": "1-2",
        "windSpeedNight": "3",
        "humidity": "56",
        "precip": "0.0",
        "pressure": "1009",
        "vis": "25",
        "cloud": "0",
        "uvIndex": "3"
      },
      {
        "fxDate": "2021-11-17",
        "sunrise": "07:01",
        "sunset": "16:57",
        "moonrise": "16:01",
        "moonset": "05:41",
        "moonPhase": "盈凸月",
        "moonPhaseIcon": "803",
        "tempMax": "13",
        "tempMin": "0",
        "iconDay": "100",
        "textDay": "晴",
        "iconNight": "150",
        "textNight": "晴",
        "wind360Day": "225",
        "windDirDay": "西南风",
        "windScaleDay": "1-2",
        "windSpeedDay": "3",
        "wind360Night": "225",
        "windDirNight": "西南风",
        "windScaleNight": "1-2",
        "windSpeedNight": "3",
        "humidity": "56",
        "precip": "0.0",
        "pressure": "1009",
        "vis": "25",
        "cloud": "0",
        "uvIndex": "3"
      },
      {
        "fxDate": "2021-11-17",
        "sunrise": "07:01",
        "sunset": "16:57",
        "moonrise": "16:01",
        "moonset": "05:41",
        "moonPhase": "盈凸月",
        "moonPhaseIcon": "803",
        "tempMax": "13",
        "tempMin": "0",
        "iconDay": "100",
        "textDay": "晴",
        "iconNight": "150",
        "textNight": "晴",
        "wind360Day": "225",
        "windDirDay": "西南风",
        "windScaleDay": "1-2",
        "windSpeedDay": "3",
        "wind360Night": "225",
        "windDirNight": "西南风",
        "windScaleNight": "1-2",
        "windSpeedNight": "3",
        "humidity": "56",
        "precip": "0.0",
        "pressure": "1009",
        "vis": "25",
        "cloud": "0",
        "uvIndex": "3"
      },
      {
        "fxDate": "2021-11-17",
        "sunrise": "07:01",
        "sunset": "16:57",
        "moonrise": "16:01",
        "moonset": "05:41",
        "moonPhase": "盈凸月",
        "moonPhaseIcon": "803",
        "tempMax": "13",
        "tempMin": "0",
        "iconDay": "100",
        "textDay": "晴",
        "iconNight": "150",
        "textNight": "晴",
        "wind360Day": "225",
        "windDirDay": "西南风",
        "windScaleDay": "1-2",
        "windSpeedDay": "3",
        "wind360Night": "225",
        "windDirNight": "西南风",
        "windScaleNight": "1-2",
        "windSpeedNight": "3",
        "humidity": "56",
        "precip": "0.0",
        "pressure": "1009",
        "vis": "25",
        "cloud": "0",
        "uvIndex": "3"
      },
      {
        "fxDate": "2021-11-17",
        "sunrise": "07:01",
        "sunset": "16:57",
        "moonrise": "16:01",
        "moonset": "05:41",
        "moonPhase": "盈凸月",
        "moonPhaseIcon": "803",
        "tempMax": "13",
        "tempMin": "0",
        "iconDay": "100",
        "textDay": "晴",
        "iconNight": "150",
        "textNight": "晴",
        "wind360Day": "225",
        "windDirDay": "西南风",
        "windScaleDay": "1-2",
        "windSpeedDay": "3",
        "wind360Night": "225",
        "windDirNight": "西南风",
        "windScaleNight": "1-2",
        "windSpeedNight": "3",
        "humidity": "56",
        "precip": "0.0",
        "pressure": "1009",
        "vis": "25",
        "cloud": "0",
        "uvIndex": "3"
      }
    ],
    "refer": {
      "sources": [
        "QWeather",
        "NMC",
        "ECMWF"
      ],
      "license": [
        "commercial license"
      ]
    }
  }
  try {
    let location = await getLocation()
    // const resFetch = await fetch(`https://devapi.qweather.com/v7/weather/${day}d?location=${location}&key=${hefengApiKey}`);
    // const res = await resFetch.json()

    return res
  } catch (error) {
  }
}

/**
 * 小时天气预报
 * @returns 
 */
export const getWeatherForHour = async (
  hour: 24 = 24
) => {

  let res: any = {
    "code": "200",
    "updateTime": "2021-02-16T13:35+08:00",
    "fxLink": "http://hfx.link/2ax1",
    "hourly": [
      {
        "fxTime": "2021-02-16T15:00+08:00",
        "temp": "2",
        "icon": "100",
        "text": "晴",
        "wind360": "335",
        "windDir": "西北风",
        "windScale": "3-4",
        "windSpeed": "20",
        "humidity": "11",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1025",
        "cloud": "0",
        "dew": "-25"
      },
      {
        "fxTime": "2021-02-16T16:00+08:00",
        "temp": "1",
        "icon": "100",
        "text": "晴",
        "wind360": "339",
        "windDir": "西北风",
        "windScale": "3-4",
        "windSpeed": "24",
        "humidity": "11",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1025",
        "cloud": "0",
        "dew": "-26"
      },
      {
        "fxTime": "2021-02-16T17:00+08:00",
        "temp": "0",
        "icon": "100",
        "text": "晴",
        "wind360": "341",
        "windDir": "西北风",
        "windScale": "4-5",
        "windSpeed": "25",
        "humidity": "11",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1026",
        "cloud": "0",
        "dew": "-26"
      },
      {
        "fxTime": "2021-02-16T18:00+08:00",
        "temp": "0",
        "icon": "150",
        "text": "晴",
        "wind360": "344",
        "windDir": "西北风",
        "windScale": "4-5",
        "windSpeed": "25",
        "humidity": "12",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1025",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-16T19:00+08:00",
        "temp": "-2",
        "icon": "150",
        "text": "晴",
        "wind360": "349",
        "windDir": "西北风",
        "windScale": "3-4",
        "windSpeed": "24",
        "humidity": "13",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1025",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-16T20:00+08:00",
        "temp": "-3",
        "icon": "150",
        "text": "晴",
        "wind360": "353",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "22",
        "humidity": "14",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1025",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-16T21:00+08:00",
        "temp": "-3",
        "icon": "150",
        "text": "晴",
        "wind360": "355",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "20",
        "humidity": "14",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1026",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-16T22:00+08:00",
        "temp": "-4",
        "icon": "150",
        "text": "晴",
        "wind360": "356",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "18",
        "humidity": "16",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1026",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-16T23:00+08:00",
        "temp": "-4",
        "icon": "150",
        "text": "晴",
        "wind360": "356",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "18",
        "humidity": "16",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1026",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-17T00:00+08:00",
        "temp": "-4",
        "icon": "150",
        "text": "晴",
        "wind360": "354",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "16",
        "humidity": "16",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1027",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-17T01:00+08:00",
        "temp": "-4",
        "icon": "150",
        "text": "晴",
        "wind360": "351",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "16",
        "humidity": "16",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1028",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-17T02:00+08:00",
        "temp": "-4",
        "icon": "150",
        "text": "晴",
        "wind360": "350",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "16",
        "humidity": "16",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1028",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-17T03:00+08:00",
        "temp": "-5",
        "icon": "150",
        "text": "晴",
        "wind360": "350",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "16",
        "humidity": "16",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1028",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-17T04:00+08:00",
        "temp": "-5",
        "icon": "150",
        "text": "晴",
        "wind360": "351",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "16",
        "humidity": "15",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1027",
        "cloud": "0",
        "dew": "-28"
      },
      {
        "fxTime": "2021-02-17T05:00+08:00",
        "temp": "-5",
        "icon": "150",
        "text": "晴",
        "wind360": "352",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "16",
        "humidity": "14",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1026",
        "cloud": "0",
        "dew": "-29"
      },
      {
        "fxTime": "2021-02-17T06:00+08:00",
        "temp": "-5",
        "icon": "150",
        "text": "晴",
        "wind360": "355",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "14",
        "humidity": "16",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1025",
        "cloud": "0",
        "dew": "-27"
      },
      {
        "fxTime": "2021-02-17T07:00+08:00",
        "temp": "-7",
        "icon": "150",
        "text": "晴",
        "wind360": "359",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "16",
        "humidity": "20",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1024",
        "cloud": "0",
        "dew": "-26"
      },
      {
        "fxTime": "2021-02-17T08:00+08:00",
        "temp": "-5",
        "icon": "100",
        "text": "晴",
        "wind360": "1",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "14",
        "humidity": "19",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1023",
        "cloud": "0",
        "dew": "-26"
      },
      {
        "fxTime": "2021-02-17T09:00+08:00",
        "temp": "-4",
        "icon": "100",
        "text": "晴",
        "wind360": "356",
        "windDir": "北风",
        "windScale": "3-4",
        "windSpeed": "14",
        "humidity": "17",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1023",
        "cloud": "0",
        "dew": "-25"
      },
      {
        "fxTime": "2021-02-17T10:00+08:00",
        "temp": "-1",
        "icon": "100",
        "text": "晴",
        "wind360": "344",
        "windDir": "西北风",
        "windScale": "3-4",
        "windSpeed": "14",
        "humidity": "14",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1024",
        "cloud": "0",
        "dew": "-26"
      },
      {
        "fxTime": "2021-02-17T11:00+08:00",
        "temp": "0",
        "icon": "100",
        "text": "晴",
        "wind360": "333",
        "windDir": "西北风",
        "windScale": "3-4",
        "windSpeed": "14",
        "humidity": "12",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1024",
        "cloud": "0",
        "dew": "-26"
      },
      {
        "fxTime": "2021-02-17T12:00+08:00",
        "temp": "1",
        "icon": "100",
        "text": "晴",
        "wind360": "325",
        "windDir": "西北风",
        "windScale": "3-4",
        "windSpeed": "14",
        "humidity": "10",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1025",
        "cloud": "16",
        "dew": "-28"
      },
      {
        "fxTime": "2021-02-17T13:00+08:00",
        "temp": "2",
        "icon": "100",
        "text": "晴",
        "wind360": "319",
        "windDir": "西北风",
        "windScale": "3-4",
        "windSpeed": "16",
        "humidity": "8",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1025",
        "cloud": "32",
        "dew": "-29"
      },
      {
        "fxTime": "2021-02-17T14:00+08:00",
        "temp": "2",
        "icon": "100",
        "text": "晴",
        "wind360": "313",
        "windDir": "西北风",
        "windScale": "3-4",
        "windSpeed": "16",
        "humidity": "9",
        "pop": "0",
        "precip": "0.0",
        "pressure": "1025",
        "cloud": "48",
        "dew": "-27"
      }
    ],
    "refer": {
      "sources": [
        "QWeather",
        "NMC",
        "ECMWF"
      ],
      "license": [
        "commercial license"
      ]
    }
  }
  try {
    let location = await getLocation()
    const resFetch = await fetch(`https://devapi.qweather.com/v7/weather/${hour}h?location=${location}&key=${hefengApiKey}`);
    res = await resFetch.json()
    return res
  } catch (error) {
  }
}